package cn.xm.lingshi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.xm.lingshi.pojo.YongHu;
import cn.xm.lingshi.service.YonghuService;
import cn.xm.lingshi.service.impl.YongHuServiceImpl;

public class YongHuServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 用户注册张厚是否可用
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void chaXunYongHu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String phone = request.getParameter("pay");
		if (phone != null && !"".equals(phone)) {
			YonghuService yhsi = new YongHuServiceImpl();
			int sum = yhsi.chaXunZhuCe(phone);
			if (sum == 0) {
				out.print(JSON.toJSONString("可以使用!"));
			} else {

				out.print(JSON.toJSONString("账号已存在!"));
			}
		}
	}

	/**
	 * 用户注册提交的表单
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void zhuCe(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String yh_ZhangHao = request.getParameter("phone");
		String yh_MiMa = request.getParameter("password");
		String yh_sfz = request.getParameter("shenfei");
		String yanzheng = request.getParameter("yanzheng");
		String shengfen = request.getParameter("shengfen");
		String chengshi = request.getParameter("chengshi");
		String dizhi = request.getParameter("dizhi");

		String yz = (String) request.getSession().getAttribute("yzm");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式

		if (yanzheng.equalsIgnoreCase(yz)) {
			YongHu yh = new YongHu();
			yh.setAccount(yh_ZhangHao);
			yh.setPassWord(yh_MiMa);
			yh.setDate(df.format(new Date()));
			yh.setCardId(yh_sfz);
			yh.setUserStatus(1);
			yh.setSite(shengfen + " " + chengshi + "" + dizhi);
			YonghuService yhs = new YongHuServiceImpl();
			int sum = yhs.tianJiao(yh);
			if (sum == 1) {
				out.print("<script>alert('注册成功！');history.back()</script>");
			} else {
				out.print("<script>alert('注册失败！');history.back()</script>");
			}
		} else {

			out.print("<script>alert('验证码错误！');history.back()</script>");
		}
	}

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void dengLu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String zhangHao = request.getParameter("userName");
		String miMa = request.getParameter("password");
		YonghuService yhs = new YongHuServiceImpl();
		YongHu yh = yhs.dengLu(zhangHao);
		if (yh != null) {
			if (miMa.equals(yh.getPassWord())) {
				request.getSession().setAttribute("denglu", yh);
				response.sendRedirect("/lingshi/index.jsp");
			}else{
				response.sendRedirect("/lingshi/index.jsp");
			}
		} else {
			response.sendRedirect("/lingshi/index.jsp");
		}
	}

	/**
	 * 用户注销
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void zhuXiao(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("denglu");
		response.sendRedirect("/lingshi/index.jsp");
	}

	/**
	 * 用户登录后台
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void dl_houTai(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		YongHu yh = (YongHu) request.getSession().getAttribute("denglu");
		if (yh.getUserStatus() == 1) {
			response.sendRedirect("order");		
		} else if (yh.getUserStatus() == 0) {
			response.sendRedirect("/lingshi/manage/index.jsp");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String typ = request.getParameter("typ");
		if (typ != null && !"".equals(typ)) {
			if (typ.equals("yanzheng")) {// 验证账号是否存在
				this.chaXunYongHu(request, response);
			} else if (typ.equals("zhuce")) {
				this.zhuCe(request, response);
			} else if (typ.equals("denglu")) {
				this.dengLu(request, response);
			} else if (typ.equals("zhuxiao")) {
				this.zhuXiao(request, response);
			} else if ("denglu_houtai".equals(typ)) {
				this.dl_houTai(request, response);
			}
		}
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
